<?php

/**
 * 处理微信公众号用户消息和系统通知
 *
 * @author pufan
 */

namespace Portal\Controller;

use Portal\Controller\CommonController;
use \Org\Util\Wechat;
use Library\Wx;

class WxcallController extends CommonController {

    public function index() {
        Wx::valid();

        $type = Wx::getRev()->getRevType();

        switch ($type) {
            case Wechat::MSGTYPE_TEXT:
                echo 'success';
                break;
            case Wechat::MSGTYPE_EVENT:
                $this->handle_event();
                echo 'success';
                break;
            case Wechat::MSGTYPE_IMAGE:
                echo 'success';
                break;
            default:
                echo 'success';
        }
    }

    /*
     * 处理微信事件推送功能
     */
    private function handle_event() {
        $wxevent = new \Library\WxEvent();
        $msg = Wx::getRevEvent();
        $event = Wx::getRevData();

        \Think\Log::record(json_encode($event));
        switch ($msg['event']) {
            case Wechat::EVENT_SUBSCRIBE: //订阅事件
                \Think\Log::record('EVENT_SUBSCRIBE');
                $wxevent->handle_subscribe($event);
                break;
            case Wechat::EVENT_UNSUBSCRIBE: //取消订阅事件
                \Think\Log::record('EVENT_UNSUBSCRIBE');
                $wxevent->handle_unsubscribe($event);
                break;
            case Wechat::EVENT_SCAN: //扫描通知
                \Think\Log::record('EVENT_SCAN');
                $wxevent->handle_scan($event);
                break;
            case Wechat::EVENT_CARD_PASS: //卡券审核通过
                $cardid = Wx::getRevCardPass();
                \Think\Log::record($cardid);
                break;
            case Wechat::EVENT_CARD_NOTPASS: //卡券审核未通过
                Wx::getRevCardPass();
                \Think\Log::record($cardid);
                break;
            case Wechat::EVENT_CARD_USER_GET: //用户领取卡券
                $event = Wx::getRevCardGet();
                \Think\Log::record(json_encode($event));
                break;
            case Wechat::EVENT_CARD_USER_DEL: //用户删除卡券
                $event = Wx::getRevCardDel();
                \Think\Log::record(json_encode($event));
                break;
            case Wechat::EVENT_SHAKEAROUNDUSERSHAKE: //摇一摇事件通知
                \Think\Log::record('EVENT_SHAKEAROUNDUSERSHAKE');
                //$wxevent->handle_shakearoundusershake($event);
                break;
            case Wechat::EVENT_LOCATION: //地理位置上报
                \Think\Log::record('EVENT_LOCATION');
                $wxevent->handle_location($event);
                break;
            default:
        }
    }

}
